package Test;

import java.util.Random;

class Node{
    private int[] arr=new int[1000];
    private volatile int size=0;
    private int start=0;
    private int end=0;
    public void put(int value) throws InterruptedException {
        synchronized (this){
            while (size==arr.length){
                wait();
            }
            arr[end]=value;
            end=(end+1)%arr.length;
            size++;
            notifyAll();
        }
    }
    public int poll() throws InterruptedException {
        int ret=0;
        synchronized (this){
            while (size==0){
                wait();
            }
            ret=arr[start];
            start=(start+1)%arr.length;
            size--;
            notifyAll();
        }
        return ret;
    }
}
public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        Node node=new Node();
        Thread t1=new Thread(()->{
            Random random=new Random();
           while (true){
               try {
                   node.put(random.nextInt(1000));
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });
        Thread t2=new Thread(()->{
            while (true){
                try {
                    int ret=node.poll();
                    System.out.println(ret);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        t2.start();

        t1.join();
        t2.join();
    }
}
